'''
Created on Aug 1, 2010

@author: behrooz
'''
from amscms.ams.models import Ad
from django.contrib.syndication.views import Feed
from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404
from amscms.dict.models import Term
from django.utils import translation
from amscms import settings

translation.activate(settings.LANGUAGE_CODE)

class BaseLatestAdsFeed(Feed):
    title_template = 'feeds/latest_ads_title.html'
    description_template = 'feeds/latest_ads_description.html'
    ttl = 10

    def author_name(self, obj):
        return _("AMSCMS Main Title")
    
    def author_email(self, obj):
        return "info@localhost"
    
    def author_link(self):
        return ""
    
    def feed_copyright(self):
        return _("Feed Copyright Notice")
    
    def item_link(self, item):
        return "/ads/view/%s/" % item.id
    
    def item_author_name(self, obj):
        return obj.advertizer.__unicode__()
    
    def item_author_email(self, obj):
        return obj.advertizer.user.email
#    
#    def item_pubdate(self, obj):
#        gt = time.mktime(obj.registration_date.timetuple())
#        jt = convert_to_jalali(gt)
#        return str(jt[0]) + "/" + str(jt[1]) + "/" + str(jt[2])
    
    def item_copyright(self, obj):
        return self.feed_copyright() + " --- " + self.author_name(obj) 
        
class LatestAdsFeed(BaseLatestAdsFeed):
    
    def title(self):
        return "%s - %s" % (_("AMSCMS Main Title"), _("Latest Ads"))
    
    def description(self):
        return _("Latest registered ads in the system")
    
    def link(self):
        return "/rss/ads/latest/"
    
    def items(self):
        ads = Ad.objects.get_default_qs().order_by("-registration_date")[:10]
        return ads
    
    
"""
Latest ads published in the system by labeled with a given tag.
"""
class LatestAdsByTagFeed(BaseLatestAdsFeed): 
    
    def title(self, obj):
        return "%s - %s: %s" % (_("AMSCMS Main Title"), _("Latest ads with tag"), obj.term) 
    
    def description(self, obj):
        return _("Latest registered ads in the system with tag: %s") % obj.term 
    
    def link(self, obj):
        return "/rss/ads/latest/tag/%s/" % obj.term
    
    def get_object(self, request, tag):
        return get_object_or_404(Term, term=tag)
    
    def items(self, obj):
        ads = Ad.objects.get_default_qs().filter(adtag__term=obj).order_by("-registration_date")[:10]
        return ads
    
   
    
